<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/5/13
 * Time: 14:24
 */
namespace Admin\Controller;
use Think\Controller;

class BannerController extends CommonController{

    public function index(){
        $title=I('get.name');
        $menu_id=$_GET['id'];
        $jur=jur_str($menu_id,$this->user_id);

        if($title){
            $where['title']=array('like',"%$title%");
        }
        $where['status']=array('neq',3);
        $count=M("banner")->count();
        $Page       = new \Think\Page($count,25);
        $banner=M("banner")->where($where)->order('add_time')->limit($Page->firstRow.','.$Page->listRows)->select();;

        $show       = $Page->show();
        $this->assign('banner',$banner);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('update',$jur['is_update']);// 赋值数据集
        $this->assign('delete',$jur['is_delete']);// 赋值数据集
        $this->breadcrumb1='基础数据';
        $this->breadcrumb2='Banner管理';
        $this->url='#';

      //  $this->assign('banner',$banner);
        $this->display();
    }

    //添加banner
    public function add(){
        //
       if($_POST){
           $data['title']=I('post.title');
           $data['type']=I('post.type');
           $data['desc']=I('post.desc');
           $data['status']=I('post.status');
           $data['add_time']=time();
           $upload = new \Think\Upload(C('UPLOAD_CONFIG'));
           $info = $upload->upload($_FILES);
           $urls = array();
           if (!$info) {// 上传错误提示错误信息
               $data=array(code=>403,msg=>$upload->getError());
               $this->ajaxReturn($data,'JSON');
           } else {// 上传成功 获取上传文件信息
               foreach ($info as $file) {
                   //$url = $file['savepath'] . $file['savename'];
                   // var_dump($info);exit;
                   array_push($urls, $file);
               }
           }
           $data['pic']=$urls[0]['url'];
           M('banner')->add($data);
           $this->success('新增成功', '/zhaoshifu/admin.php?s=/Banner/index');
       }else{
           $this->breadcrumb2='添加';
           $this->breadcrumb1='Banner管理';
           $this->url=U('Banner/index');
           $this->display();
       }
    }

    //删除banner
    public function delete(){
        $ids=I('post.ids');
        if(!$ids){
            $this->error('选择不能为空');
            return;
        }
        $ids=implode(',',$ids);
        $where['id']=array('in',$ids);
        $save['status']=3;
        M('banner')->where($where)->save($save);
        $this->success('删除成功', '/zhaoshifu/admin.php?s=/Banner/index');
    }

    /**
     * 编辑
     */
    public function edit(){
        if($_POST){
           $data['title']=I('post.title');
           $data['desc']=I('post.desc');
           $data['type']=I('post.type');
           $data['status']=I('post.status','2');
           $data['url']=I('post.url','');
           $id=I('post.id');

            if(empty($data['title'])){
                $this->error('标题不能为空', '/zhaoshifu/admin.php?s=/Banner/edit&id='.$id);
            }

            if(empty($data['desc'])){
                $this->error('描述不能为空', '/zhaoshifu/admin.php?s=/Banner/edit&id='.$id);
            }
            if(empty($data['type'])){
                $this->error('类型不能为空', '/zhaoshifu/admin.php?s=/Banner/edit&id='.$id);
            }

            if(empty($id)){
                $this->error('出错了', '/zhaoshifu/admin.php?s=/Banner/edit&id='.$id);
            }


            if($_FILES['icon']['size']){
                $upload = new \Think\Upload(C('UPLOAD_CONFIG'));
                $info = $upload->upload($_FILES);
                $urls = array();
                if (!$info) {// 上传错误提示错误信息
                    $data=array(code=>403,msg=>$upload->getError());
                    $this->ajaxReturn($data,'JSON');
                } else {// 上传成功 获取上传文件信息
                    foreach ($info as $file) {
                        //$url = $file['savepath'] . $file['savename'];
                        // var_dump($info);exit;
                        array_push($urls, $file);
                    }
                }
                $data['pic']=$urls[0]['url'];
            }

            M('banner')->where("id=$id")->save($data);
            $this->success('修改成功', '/zhaoshifu/admin.php?s=/Banner/edit&id='.$id);
        }else{
            $id=I('get.id');
            $detail=M('banner')->where("id=$id")->find();
            if(empty($detail)){
                $this->error('出错了', '/zhaoshifu/admin.php?s=/Banner/index');
            }
            $this->assign('detail',$detail);
            $this->display();
        }
    }


    /**
     * banner分类
     */
    public function bannerType(){

    }
}